﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public abstract class hClickableRect:hWindow
    {
        protected bool _isSelected = false;
        protected bool _isHightlighted = false;
        protected String _text = "";
        protected hGlobal.Command _command = hGlobal.Command.Undefined;
        protected hText _console;
        protected bool _acceptClick=true;        
        private bool _selectingDelay = false;
        private bool _visible = true;

        #region Properties

        public bool Visible
        {
            get { return _visible; }
            set { _visible = value; }
        }

        /// <summary>
        /// Tạm dừng để vẽ khi chọn trước khi gửi thông tin về việc đã chọn ra ngoài
        /// </summary>
        protected bool SelectingDelay
        {
            set 
            {
                _selectingDelay = value;
                if(value==true && _sprite[0].NTextures>=3)
                    _sprite[0].CurrentTextureIndex = 2;
            }
            get { return _selectingDelay; }
        }

        public hText Console 
		{
			set { _console = value; }
            get { return _console; }
		}
        
        public String Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public hGlobal.Command Command
        {
            get { return _command; }
            set { _command = value; }
        }

        public bool Selected
        {
            get { return _isSelected; }
            set 
            { 
                _isSelected = value; 
                if(Selected && _sprite[0].NTextures>=3)
                    _sprite[0].CurrentTextureIndex=2;
                else
                    _sprite[0].CurrentTextureIndex=0;
            }
        }

        public bool Hightlighted
        {
            get { return _isHightlighted; }
            set
            {
                _isHightlighted = value;
                if (_isHightlighted && _sprite[0].NTextures>=5)
                    _sprite[0].CurrentTextureIndex=4;
                else
                    _sprite[0].CurrentTextureIndex=0;
            }
        }

        #endregion

        /// <summary>
        /// Nút dịch chuyển khi click vào
        /// </summary>
        /// <param name="undo"></param>
        public abstract void moveOnClicked(bool undo);
        
        public int nDelay = 10;
        protected int iDelay = 1;

        public new void update(hParams args, GameTime gameTime)
        {
            if (Command == hGlobal.Command.Undefined && Text.Equals(""))
            {
                Visible = false;
                return;
            }

            if (SelectingDelay)            
            {
                moveOnClicked(false);
                if (iDelay == 0)
                {
                    moveOnClicked(true);
                    SelectingDelay = false;
                    Selected = true; 
                }
                iDelay = (iDelay + 1) % nDelay;
                return;
            }

            _isHightlighted = false;
            _isSelected = false;
            
            MouseState mouse = Mouse.GetState();

            if (mouse.X > X && mouse.X < X + Width
            && mouse.Y > Y && mouse.Y < Y + Height)
            {
                if (mouse.LeftButton == ButtonState.Pressed && _acceptClick)
                {
                    _acceptClick = false;
                    SelectingDelay = true;
                }
                else
                {
                    Hightlighted = true;
                }
            }
            else
            {
                Hightlighted = false;
                Selected = false;
            }

            if (mouse.LeftButton == ButtonState.Released && _acceptClick==false)
            {
                _acceptClick = true;             
            }
        }

        public new void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (Visible)
            {
                base.draw(gameTime, spriteBatch);
                _console.print(gameTime, spriteBatch, this.X, this.Y, this.Width, this.Height, this.Text, Color.Black, hGlobal.FontStyle.Bold);
            }
        }

        public new void draw(GameTime gameTime, SpriteBatch spriteBatch, int alpha)
        {
            if (Visible)
            {
                base.draw(gameTime, spriteBatch, alpha);
                _console.print(gameTime, spriteBatch, this.X, this.Y, this.Width, this.Height, this.Text, Color.Black, hGlobal.FontStyle.Bold);
            }
        }

        public void draw(GameTime gameTime, SpriteBatch spriteBatch, int alpha, hGlobal.FontStyle fontStyle)
        {
            if (Visible)
            {
                base.draw(gameTime, spriteBatch, alpha);
                _console.print(gameTime, spriteBatch, this.X, this.Y, this.Width, this.Height, this.Text, Color.Black, fontStyle);
            }
        }

        public void draw(GameTime gameTime, SpriteBatch spriteBatch, int alpha, Color color , hGlobal.FontStyle fontStyle)
        {
            if (Visible)
            {
                base.draw(gameTime, spriteBatch, alpha);
                _console.print(gameTime, spriteBatch, this.X, this.Y, this.Width, this.Height, this.Text, color, fontStyle);
            }
        }

        public void createButton(ContentManager content, float x, float y)
        {
            string[] txt = 
                { 
                    hGlobal.PathGui + hGlobal.ButtonMini, 
                    hGlobal.PathGui + hGlobal.ButtonDisableMini, 
                    hGlobal.PathGui + hGlobal.ButtonSelectedMini, 
                    hGlobal.PathGui + hGlobal.ButtonLightGreenMini, 
                    hGlobal.PathGui + hGlobal.ButtonLightYellowMini 
                };
            create1SpriteObject(content, txt);
            X = x;
            Y = y;  
        }

        public void createMenuItem(ContentManager content, float x, float y)
        {
            string[] txt =
                { 
                    hGlobal.PathGui + hGlobal.Button, 
                    hGlobal.PathGui + hGlobal.ButtonDisable, 
                    hGlobal.PathGui + hGlobal.ButtonSelected, 
                    hGlobal.PathGui + hGlobal.ButtonLightGreen,
                    hGlobal.PathGui + hGlobal.ButtonLightYellow
                };
            create1SpriteObject(content, txt);
            X = x;
            Y = y;
        }

        //public void createActionButton(ContentManager content, float x, float y)
        //{
        //    string[] txt = {hGlobal.PathGui+ hGlobal.ActionButtonUndefined};
        //    create1SpriteObject(content, txt);
        //    X = x;
        //    Y = y;
        //}

        public void createActionButton(ContentManager content, string strtexture ,float x, float y)
        {
            string[] txt = { hGlobal.PathGui + strtexture };
            create1SpriteObject(content, txt);
            X = x;
            Y = y;
        }
    }
}
